<?php
                require 'db.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> miehaha </title>
<meta name="keywords" content="" />
<meta name="description" content=" mufafa" />
<link rel="stylesheet" type="text/css"  href="/css/main.css"  />
<link rel="stylesheet" type="text/css"  href="jquery.tooltip.css"  />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tooltip.min.js"></script> 
<script src="jquery.masonry.min.js"></script>
<script type="text/javascript" src="masonry/sc.js"></script> 
<script type="text/javascript" src="masonry/jquery.infinitescroll.min.js"></script> 
<style>
   #content .item{
        border :2px black solid;
width : 100%;
        margin-top: 10px;
    }
    .imga{
    padding : 5px;
}
.da{
    text-decoration:none;
    font-size: 22px;
    padding: 5px;
}
</style>
</head>
        
        <body>
            <?php
                
                $ttype = '1';
				if (array_key_exists( 'ttype', $_GET)){
                    $ttype = $_GET['ttype'];
                }
                $tpl = "<div class='item'><a class='da' href='index.php?fid=143,4,230&page=1&kw=%s' target=_blank><b>%s(%s)</b></a><br/>%s</div>"; 
                $sql = 'select tag_name, tag_type, tag_count from tag where tag_type = %s order by tag_count desc ;';
                $sql = sprintf($sql, $ttype);
            ?>
          
          
            
            <?php
                require 'header.php';
             ?>
             
            
             
          
          <div id='content'>
              <?php
                // 查询tag
            $mysqli->real_query($sql);
            $result = $mysqli->use_result(); 
            $tags = array();
            if ($result) {
                while ($row = $result->fetch_row()){
                    array_push($tags, $row);

                }
                $result->close();
            }
            foreach ($tags as $row){
                // 查询post, 取最新3张图片
                $name = $row[0];
                $type = $row[1];
                $count = $row[2];
                $sql2 = 'select a.code, a.title, a.preview from  post a where a.pid in (select pid from tag_rela where  tag_type=%s and tag_key="%s" order by pid desc ) limit 3';
                $sql2 = sprintf($sql2, $type, sprintf('%s_%s', $name, $type) );
                $mysqli->real_query($sql2);
                $result2 = $mysqli->use_result(); 
                if ($result2){
                    $prvtpl = '<a class="imga" href="post.php?code=%s" target=_blank ><img title="%s" src="%s" ></img></a>';
                    $prvhtml = '';
                    while ($row = $result2->fetch_row()){
                        $code = $row[0];
                        $title = $row[1];
                        $preview = $row[2];
                        $imghost = 'http://img%d'.$__host.'/datas';
                        $hostn = rand(1,7);
                        $imghost = sprintf($imghost, $hostn);
                        $preview = $imghost.$preview;
                        $prvhtml .= sprintf($prvtpl, $code, $title, $preview);
                    }
                    $link = sprintf($tpl, $name, strtoupper($name), $count, $prvhtml);
                    echo $link;
                }
            }
                ?>
          </div>
          
            
          <div id='footer'></div>
        
        <script>
        </script>
          
        </body>
        
</html>
